'use strict';

// ajax函数将返回Promise对象:
function ajax(method, url, data) {
    var request = new XMLHttpRequest();
    request.open(method, url);
    request.send(data);
    return new Promise(function (resolve, reject) {
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                if (request.status === 200) {
                    resolve(request.responseText);
                } else {
                    reject(request.status);
                }
            }
        }
    })
}

// var p = ajax('GET', 'data.json');
// p.then(function (response) { // 如果AJAX成功，获得响应内容
//     console.log('CONTENT: ' + response);
// }).catch(function (status) { // 如果AJAX失败，获得响应代码
//     console.log('ERROR: ' + status);
// });